<?php
namespace app\bis\controller;
use phpmailer\Email;
use think\Controller;
class Register extends  Controller
{
    public function  index(){
        $citys = model('City')->getNormalCitysByParentId();
        $categorys = model('Category')->getNormalCategoryByParentId();
        return $this->fetch('',[
            'citys' =>$citys,
            'categorys' =>  $categorys
        ]);
    }

    public function add(){
        if(!request()->isPost()){
            $this->error('请求错误');
        }
        $data = input('post.','',"htmlentities");
       $validate = validate('Bis');
     /*   if(!$validate->scene('add')->check($data)){
            $this->error($validate->getError());
        }*/
       $lng = \Map::getLngLat($data['address']);
       /* if(empty($lng) || $lng['status'] !=0 || $lng['result']['precise'] !=1){
            $this->error('无法获取数据或者不精准');
        }*/

       //判断用户是否存在
       $result =  model('BisAccount')->get(['username'=>$data['username']]);
        if($result){
            $this->error('用户存在');
        }

      //bis表
        $bisData = [
            'name' => htmlentities($data['name']),
            'city_id' => $data['city_id'],
            'city_path' => empty($data['se_city_id']) ? $data['city_id'] : $data['city_id'].','.$data['se_city_id'],
            'description' => empty($data['description']) ? '' : $data['description'],
            'bank_info' => $data['bank_info'],
            'bank_user' => $data['bank_user'],
            'bank_name' => $data['bank_name'],
            'faren'=> $data['faren'],
            'faren_tel' => $data['faren_tel'],
            'email'=> $data['email']
        ];
        $bisId = model('Bis')->add($bisData);

        //总店相关信息
        $data['cat'] = '';
        if(!empty($data['se_category_id'])){
            $data['cat'] = implode("|",$data['se_category_id']);
        }
        $locationData = [
            'bis_id' => $bisId,
            'name' => $data['name'],
            'tel' => $data['tel'],
            'contact' => $data['contact'],
            'category_id' => $data['category_id'],
            'category_path' => $data['category_id']. ',' . $data['cat'],
            'city_path' => empty($data['se_city_id']) ? $data['city_id'] : $data['city_id'].','.$data['se_city_id'],
            'address' => $data['address'],
            'open_time' => $data['open_time'],
            'content' => empty($data['content']) ? '' : $data['content'],
            'is_main' => 1,
            'xpoint' => empty($lng['result']['location']['lng']) ? '' : $lng['result']['location']['lng'],
            'ypoint' => empty($lng['result']['location']['lat']) ? '' : $lng['result']['location']['lat'],
        ];
        $locationId  = model('BisLocation')->add($locationData);


        $data['code'] = mt_rand(100,10000);
        $accountData = [
            'bis_id' => $bisId,
            'username' => $data['username'],
            'code' => $data['code'],
            'password' => md5($data['password'].$data['code']),
            'is_main' => 1,
        ];
        $accountId = model('BisAccount')->add($accountData);
        if(!$accountId){
            $this->error('申请失败');
        }
        $url=request()->domain().url('bis/register/waiting',['id'=>$bisId]);
        $title ="o2o申请通知";
        $content = "提交的申请等待审核，点击链接查看<a href='".$url."' target='_blank'>查看链接</a>";
        \phpmailer\Email::send($data['email'],$title,$content);
        $this->success('申请成功',url('register/waiting'),['id'=>$bisId]);
    }

    public function waiting($id){
        if(empty($id)){
            $this->error('error');
        }
        $detail = model('Bis')->get($id);
        return $this->fetch('',[
            'detail' => $detail,
        ]);
    }
}